import Vue from 'vue'
import App from './App.vue'
import router from './router'
import './plugins/element.js'
// 导入全局样式表
import './assets/css/global.css'

// 导入axios
import axios from 'axios'

// 导入moment时间格式化
import moment from 'moment'

import store from './store'
import ElementUI from 'element-ui'

// 配置 axios 请求根路径
axios.defaults.baseURL = 'http://localhost:8082/'

// 配置 axios 过期时间
axios.defaults.timeout = 1000000
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';

// 添加请求拦截器
axios.interceptors.request.use(request => {
    request.headers.Authorization = localStorage.getItem('token')
    return request
})

// 添加响应拦截器
axios.interceptors.response.use(response => {
    if (response.data.code === 200) {
        return response
    } else {
        console.log(response)
        ElementUI.Message.error(response.data.message ? response.data.message : '系统异常')
        return Promise.reject(response.data.message)
    }
}, error => {
    if (error.response.data) {
        error.message = error.response.data.message
    }

    if (error.response.status === 401) {
        router.push('/login')
    }

    if (error.response.status === 403) {
        router.go(-1)
    }

    ElementUI.Message.error(error.message ? error.message : '系统异常')
    return Promise.reject(error)
})

// 把 axios 挂载到vue原型对象上
Vue.prototype.$axios = axios
Vue.prototype.$moment = moment

Vue.config.productionTip = false
new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')